package com.ruoyi.mdm.service.impl;

import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.Assert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.mdm.domain.dto.MaterialCategoryDTO;
import com.ruoyi.mdm.domain.dto.UnitDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mdm.mapper.UnitMapper;
import com.ruoyi.mdm.domain.Unit;
import com.ruoyi.mdm.service.IUnitService;

import javax.annotation.Resource;

/**
 * 单位字典Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-03
 */
@Service
public class UnitServiceImpl implements IUnitService {
	@Resource
	private UnitMapper unitMapper;

	/**
	 * 查询单位字典
	 *
	 * @param tableId 单位字典主键
	 * @return 单位字典
	 */
	@Override
	public Unit selectUnitByTableId(Long tableId) {
		return unitMapper.selectUnitByTableId(tableId);
	}

	/**
	 * 查询单位字典列表
	 *
	 * @param unit 单位字典
	 * @return 单位字典
	 */
	@Override
	public List<Unit> selectUnitList(Unit unit) {
		return unitMapper.selectUnitList(unit);
	}

	/**
	 * 新增单位字典
	 *
	 * @param unit 单位字典
	 * @return 结果
	 */
	@Override
	public int insertUnit(Unit unit) {
		unit.setCreateTime(DateUtils.getNowDate());
		return unitMapper.insertUnit(unit);
	}

	/**
	 * 修改单位字典
	 *
	 * @param unit 单位字典
	 * @return 结果
	 */
	@Override
	public int updateUnit(Unit unit) {
		unit.setUpdateTime(DateUtils.getNowDate());
		unit.setTableId(IdUtils.fastId());
		return unitMapper.updateUnit(unit);
	}

	/**
	 * 批量删除单位字典
	 *
	 * @param tableIds 需要删除的单位字典主键
	 * @return 结果
	 */
	@Override
	public int deleteUnitByTableIds(Long[] tableIds) {
		return unitMapper.deleteUnitByTableIds(tableIds);
	}

	/**
	 * 删除单位字典信息
	 *
	 * @param tableId 单位字典主键
	 * @return 结果
	 */
	@Override
	public int deleteUnitByTableId(Long tableId) {
		return unitMapper.deleteUnitByTableId(tableId);
	}

	public String importData(List<UnitDTO> dataList, boolean updateSupport) {
		Assert.isTrue(!dataList.isEmpty(), "数据列表为空");

		// 查询出所有单位组，及单位组下的所有单位
		List<String> unitGroupList = dataList.stream().map(UnitDTO::getUnitGroup).collect(Collectors.toList());
		Map<String, List<Unit>> unitGroup = new ArrayList<Unit>().stream().collect(Collectors.groupingBy(Unit::getUnitGroup));
		Map<String, List<UnitDTO>> unitDTOGroup = dataList.stream().collect(Collectors.groupingBy(UnitDTO::getUnitGroup));
		unitDTOGroup.forEach((groupCode, unitDTOList) -> {
			List<Unit> dtoList = unitDTOList.stream().map(UnitDTO::toPO).collect(Collectors.toList());
			List<Unit> unitList = unitGroup.getOrDefault(groupCode, new ArrayList<>());
			unitList.addAll(dtoList);
			unitList.sort(Comparator.comparing(Unit::getSortNo));
			for (int i = 0; i < unitList.size(); i++) {
				unitList.get(i).setSortNo(i);
			}

			List<Unit> baseList = unitList.stream().filter(item -> Integer.valueOf(1).equals(item.getBaseFlag())).collect(Collectors.toList());
			Assert.isTrue(!baseList.isEmpty(), "需要指定分组内的基准单位");
			Assert.isTrue(baseList.size() == 1, "分组内的基准单位只能有1个");
			Unit baseUnit = baseList.get(0);
			for (int i = baseUnit.getSortNo() + 1; i < unitList.size(); i++) {
				Unit nextUnit = unitList.get(i);
				Unit preUnit = unitList.get(i - 1);
				nextUnit.setBaseConvertRate(preUnit.getAdjoinConvertRate().multiply(preUnit.getBaseConvertRate()));
			}

			for (int i = baseUnit.getSortNo() - 1; i > 0; i--) {
				Unit currUnit = unitList.get(i);
				Unit nextUnit = unitList.get(i + 1);
				nextUnit.setBaseConvertRate(nextUnit.getBaseConvertRate().divide(currUnit.getAdjoinConvertRate(), 12, RoundingMode.DOWN));
			}

		});

		return null;
	}
}
